View Javadoc
1   /*
2    * ListCollection.java
3    *
4    * Created on December 21, 2004, 9:24 AM
5    */
6   
7   package gov.noaa.eds.xapi.generic;
8   
9   import gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService;
10  import java.util.Hashtable;
11  import org.xmldb.api.base.Collection;
12  import org.xmldb.api.base.ErrorCodes;
13  import org.xmldb.api.base.Resource;
14  import org.xmldb.api.base.Service;
15  import org.xmldb.api.base.XMLDBException;
16  
17  /***
18   * An implementation of Collection which uses a hashtable to map resource ids
19   * to resource objects.
20   * @version $Id: GenericCollection.java,v 1.2 2004/12/23 22:26:01 mrxtravis Exp $
21   * @author tns
22   */
23  public class GenericCollection extends GenericConfigurable implements Collection {
24      
25      private Hashtable resources = new Hashtable();
26      
27      private Service[] services;
28      
29      private String name = null;
30      int nextId = 1;
31      Object nextIdMutex = new Object();
32      
33      /*** Creates a new instance of ListCollection */
34      public GenericCollection() {
35          initServices();
36      }
37      
38      protected void initServices(){
39          services = new Service[1];
40          GenericXPathQueryService service = new GenericXPathQueryService();
41          service.setCollection(this);
42          services[0] = service;
43      }
44  
45      /*** Not implemented, just throws an exception
46       *@todo Figure out child relationships and implement getChildCollection
47       *@throws XMLDBException all the time
48       */
49      public Collection getChildCollection(String str) throws XMLDBException {
50          throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
51      }
52  
53      /***Returns a resource if it is in the resources hash
54       *@param id The id of the resource to retrieve
55       *@return The resource with the specified id
56       */
57      public Resource getResource(String id) throws XMLDBException {
58          Resource resource = (Resource) resources.get(id);
59          return resource;
60      }
61  
62      /***Store the resource in the collection 
63       *@param resource The resource to store
64       */
65      public void storeResource(Resource resource) throws XMLDBException {
66          resources.put(resource.getId(),resource);
67      }
68  
69      /*** Removes the resource from the hash of resources 
70       *@param resource The resource to remove
71       */
72      public void removeResource(Resource resource) throws XMLDBException {
73          resources.remove(resource.getId());
74      }
75  
76      /*** Returns a list of Resource ids
77       *@return One per string
78       */
79      public String[] listResources() {
80          return (String[]) resources.keySet().toArray(new String[0]);
81      }
82  
83      /***Does nothing, it never closes.  This collection instance is shared*/
84      public void close() {
85          //this never closes.
86      }
87  
88      /*** Returns a random number that is not currently taken.
89       *@return The id
90       */
91      public String createId() {
92          synchronized (nextIdMutex){
93              String createdId = Integer.toString(nextId++);
94              while (resources.containsKey(createdId)){
95                  createdId = Integer.toString(nextId++);
96              }
97              return createdId;
98          }
99      }
100 
101     /*** Not implemented, throws Exception 
102      *@todo Create a GenericXmlNodeResource when asked
103      */
104     public Resource createResource(String str, String str1) throws XMLDBException {
105         throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
106     }
107 
108     /***Not implemented, thorws Exception 
109      *@todo Figure out child collections and implement this method
110      *@throws XMLDBException with a NOT_IMPLEMENTED code
111      */
112     public int getChildCollectionCount() throws XMLDBException {
113         throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
114     }
115 
116     /***Return the name of this collection 
117      *@return the collection name
118      */
119     public String getName() {
120         return this.name;
121     }
122     
123     /*** Set the name of this collection 
124      *@param name The name of this collection.
125      */
126     public void setName(String name){
127         this.name = name;
128     }
129 
130     /*** Exception thrown
131      *@throws XMLDBException with a NOT_IMPELEMNTED code
132      *@todo figure out relationship to parent and implement this method
133      */
134     public Collection getParentCollection() throws XMLDBException {
135         throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
136     }
137 
138     /*** Returns the number of resources 
139      *@return the number of resource in this collection
140      */
141     public int getResourceCount() {
142         return this.resources.size();
143     }
144 
145     /***Returns the available service with the specified name and version.
146      *@param name The name of the service, like "XPathQueryService"
147      *@param version The version of the service, like "1.0"
148      *@throws XMLDBException if there is not a service that matches the 
149      *requested service
150      *@return The service that matches the requested service
151      */
152     public Service getService(String name, String version) throws XMLDBException {
153         for (int i = 0; i < services.length; i++){
154             if (name.equals(services[i].getName()) && version.equals(services[i].getVersion())){
155                 return services[i];
156             }
157         }
158         throw new XMLDBException(ErrorCodes.NO_SUCH_SERVICE);
159     }
160 
161     /***Returns all know services of this collection
162      *@return A list of Service object
163      */
164     public Service[] getServices() {
165         return this.services;
166     }
167 
168     /*** Always open 
169      *@return true
170      */
171     public boolean isOpen() {
172         return true;
173     }
174 
175     /***throws exceptioin
176      *@todo Figure out child collections and implement this method
177      *@thorws XMLDBException with {@link ErrorCodes.NOT_IMPLEMENTED} code.
178      */
179     public String[] listChildCollections() throws XMLDBException {
180         throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
181     }
182     
183 }